import {
  C0LUMNS,
  PAY_USE,
  ISDELAYPAY_TYPE,
  BUS_MODEL,
  PAYMENT_STATUS
} from './index.const';
import {
  findPaymentByOrderId,
  orderProductList
} from '@api/m_mall_web_v1.js';
import {
  urlDownload
} from '@const/utils.js';

import ElEditable from '@components/el-editable';
export default {
  components: {
    ElEditable
  },
  inject: ['apiMMallWeb'],
  data() {
    return {
      isShowText: true,
      timeOut: '00:00:00',
      toPayDialogVisible: false,
      PAY_USE,
      ISDELAYPAY_TYPE,
      BUS_MODEL,
      PAYMENT_STATUS,
      currentPage: 1,
      pageSize: 20,
      tableLoading: false,
      payload: {},
      C0LUMNS,
      detailData: {}
    };
  },
  created() {
    this.getDetail()
    this.setTime()

  },
  computed: {
    orderId() {
      return this.$route.query.id;
    },
    orderStatus() {
      return this.$route.query.orderStatus;
    },
  },
  methods: {
    //获取详情
    getDetail() {
      this.$axios.$get(`${findPaymentByOrderId}?orderId=${this.orderId}`).then(res => {
        if (res.payload) {
          let payload = res.payload
          payload.payUse ? payload.payUse = PAY_USE[payload.payUse] : payload.payUse = '-'
          payload.busModel ? payload.busModel = BUS_MODEL[payload.busModel] : payload.busModel = '-'
          payload.paymentStatus ? payload.paymentStatus = PAYMENT_STATUS[payload.paymentStatus] : payload.paymentStatus = '无'
          this.detailData = payload
          this.getDetailList()

        }
      })
    },
    //获取详情商品列表
    getDetailList() {
      this.tableLoading = true
      this.$axios.$post(orderProductList, {
        orderDetailId: this.detailData.orderDetailId,
        page: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        this.payload = res.payload;
        this.tableLoading = false
      })
    },
    setTime() {
      const endTime = new Date("2020/07/25 15:45:00").getTime();
      const staTime = new Date().getTime()
      if (endTime > staTime) {
        this.initNavButtons(true)
        let that = this
        let timer = setInterval(function () {
          let time = new Date().getTime()
          let time_distance = endTime - time
          // 时
          var int_hour = Math.floor(time_distance / 3600000);
          time_distance -= int_hour * 3600000;
          // 分
          var int_minute = Math.floor(time_distance / 60000);
          time_distance -= int_minute * 60000;
          // 秒
          var int_second = Math.floor(time_distance / 1000);
          // 时分秒为单数时、前面加零
          if (int_hour < 10) {
            int_hour = "0" + int_hour;
          }
          if (int_minute < 10) {
            int_minute = "0" + int_minute;
          }
          if (int_second < 10) {
            int_second = "0" + int_second;
          }
          if (int_hour == 0 && int_minute == 0 && int_second == 0) {
            that.timeOut = "00:00:00"
            that.isShowText = false
            that.initNavButtons(false)
            clearInterval(timer)

          } else {
            that.timeOut = `${int_hour}:${int_minute}:${int_second}`;
          }
        }, 1000)
      } else {
        this.initNavButtons(false)
        this.isShowText = false
      }
    },
    dowload() {
      urlDownload('https://md.heng-tai.com.cn/guoke-channel-aggregation-center/api/v1/files/download/wd194s2ehyzbl9vpqklq2rm7uweb5s3x.exe')
    },
    // 初始化 导航栏 按钮组
    initNavButtons(val) {
      let btn = {
        type: 'btn',
        arr: []
      }
      if (val) {
        btn.arr.push({
          type: 'primary',
          val: '去支付',
          handle: this.toPay
        })
      }
      this.$store.commit('changeLayoutHandle', btn)
    },
    toPay() {
      this.toPayDialogVisible = true
    },
    submitPay() {
      this.$axios.$post('m-mall-center/api/v1/payBillManage/InitiatePayment', {
        orderId: this.orderId
      }).then(res => {
        if (res.code === '0') {
          let url = res.payload.url.replace('&', 'separator')
          window.open(`alert:${url}`)
          this.$router.replace({
            path: '/financial-management/accounts-payable-managemen/pay/paying',
            query: {
              id: this.orderId,
              seqNo: res.payload.seqNo
            }
          });
        }
      })
      // window.open(`alert:https://cpay3.dccnet.com.cn/corporpay/servlet/ICBCCPayBusinessServlet?unique_serialno=2020073118064545200000000000547036&signstr=8B456C8EED181922FAD2A98FDD334E4FCE3EADE7F835B312EF3C1907A6EB9812D5CEBBF7FCE625D2`)
      // this.$router.replace({
      //   path: '/financial-management/accounts-payable-managemen/pay/paying',
      //   query: {
      //     id: this.orderId,
      //     seqNo: 'ZL202007241806360015'
      //   }
      // });

    },
    reloadPagination(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getDetailList()
    }
  }
};